package com.wanmait.employment.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.employment.entity.Company;
import com.wanmait.employment.entity.CompanyPic;
import com.wanmait.employment.mapper.CompanyMapper;
import com.wanmait.employment.mapper.CompanyPicMapper;
import com.wanmait.employment.service.CompanyPicService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CompanyPicServiceImpl implements CompanyPicService {

    @Resource
    private CompanyPicMapper companyPicMapper;
    @Resource
    private CompanyMapper companyMapper;
    @Override
    public PageInfo<CompanyPic> selectList(String title,Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        LambdaQueryWrapper<CompanyPic> companyPicLambdaQueryWrapper=new LambdaQueryWrapper<>();
        companyPicLambdaQueryWrapper.inSql(CompanyPic::getCompanyId,"Select id from company where company_name like '%"+title+"%'");
        List<CompanyPic> companyPics=companyPicMapper.selectList(companyPicLambdaQueryWrapper);
        PageInfo<CompanyPic> companyPicPageInfo=new PageInfo<>(companyPics);
        companyPicPageInfo.getList().forEach(companyPic->{
            QueryWrapper<Company> companyQueryWrapper=new QueryWrapper<>();
            companyQueryWrapper.eq("id",companyPic.getCompanyId());
            Company company= companyMapper.selectOne(companyQueryWrapper);
            companyPic.setCompany(company);
        });
        return companyPicPageInfo;
    }

    @Override
    public void delete(Integer id) {
        companyPicMapper.deleteById(id);
    }
}
